<html>
<head><meta charset="utf-8"><title>Deriving what constitutes legal borrows · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Deriving.20what.20constitutes.20legal.20borrows.html">Deriving what constitutes legal borrows</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="245190147"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Deriving%20what%20constitutes%20legal%20borrows/near/245190147" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> jmanchuck <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Deriving.20what.20constitutes.20legal.20borrows.html#245190147">(Jul 07 2021 at 14:36)</a>:</h4>
<p>Is there a way to 'derive' or come up with a simple set of rules that defines when borrows are legal? I'm trying to generate Rust programs that respect the ownership rules of the language, which in a sense would be reverse engineering the borrow checker but ideally at a simpler level (still quite new to Rust). I've watched Nell's video on the borrow checker and read through the section in the guide to rust development but still haven't got a clue. Or are the rules listed on <a href="https://rustc-dev-guide.rust-lang.org/borrow_check.html">https://rustc-dev-guide.rust-lang.org/borrow_check.html</a> sufficient?</p>



<a name="245191745"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Deriving%20what%20constitutes%20legal%20borrows/near/245191745" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Deriving.20what.20constitutes.20legal.20borrows.html#245191745">(Jul 07 2021 at 14:46)</a>:</h4>
<p>as far as i can tell, they're not fully specified except in the compiler source code. Stacked Borrows is a proposed memory for Rust that possibly is a superset of the borrow checker rules. the NLL RFC <a href="https://rust-lang.github.io/rfcs/2094-nll.html">https://rust-lang.github.io/rfcs/2094-nll.html</a> looks like it contains many but not all of the rules. (it doesn't fully explain reborrows, and is more about validating lifetime constraints than about deciding when a borrow is permitted)</p>



<a name="245192508"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Deriving%20what%20constitutes%20legal%20borrows/near/245192508" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Deriving.20what.20constitutes.20legal.20borrows.html#245192508">(Jul 07 2021 at 14:50)</a>:</h4>
<p><a href="https://doc.rust-lang.org/nightly/reference/expressions/operator-expr.html#borrow-operators">https://doc.rust-lang.org/nightly/reference/expressions/operator-expr.html#borrow-operators</a> contains some of the rules, but i also think it's ambiguous. (i've concluded that the borrow checker tracks borrows against places (lvalues), not memory locations, even though lots of text conflates the two. the Stacked Borrows model tracks memory locations, but it is not the borrow checker.)</p>



<a name="245194591"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Deriving%20what%20constitutes%20legal%20borrows/near/245194591" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Yu [they/she] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Deriving.20what.20constitutes.20legal.20borrows.html#245194591">(Jul 07 2021 at 15:02)</a>:</h4>
<p>for example, reborrowing allows you to create multiple in-scope mutable references to the same memory location, but not the same lvalue. (the first mutable reference is considered borrowed and is temporarily invalidated until the end of the lifetime of the second one)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>